package cn.com.css.taxInspection.workflow.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.com.css.taxInspection.gt3.entity.JcAjztSlytzEntity;
import cn.com.css.taxInspection.gt3.entity.ZxyhjEntity;
import cn.com.css.taxInspection.gt3.service.JcAjtzSlytzService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.metadata.PageList;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.springframework.stereotype.Controller;

import cn.com.css.taxInspection.common.aop.annotation.LogAnnotation;
import cn.com.css.taxInspection.common.constants.Constants;
import cn.com.css.taxInspection.common.result.DataResult;
import cn.com.css.taxInspection.gt3.entity.DmQxSwrysfEntity;
import cn.com.css.taxInspection.gt3.entity.JcAjxxEntity;
import cn.com.css.taxInspection.gt3.service.DmQxSwrysfService;
import cn.com.css.taxInspection.gt3.service.JcAjxxService;
import cn.com.css.taxInspection.system.mapper.SysUserMapper;
import cn.com.css.taxInspection.system.service.impl.HttpSessionService;
import cn.com.css.taxInspection.util.TsBqUtils;
import cn.com.css.taxInspection.workflow.constants.WorkflowConstants;
import cn.com.css.taxInspection.workflow.entity.WorkflowDebugtaxEntity;
import cn.com.css.taxInspection.workflow.entity.WorkflowInspectionHearEntity;
import cn.com.css.taxInspection.workflow.service.WorkflowDebugtaxService;
import cn.com.css.taxInspection.workflow.service.WorkflowInspectionHearService;
import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping("/workflow/hear")
public class WorkflowHearController {

    @Autowired
    private HttpSessionService httpSessionService;

    @Autowired
    private WorkflowInspectionHearService workflowInspectionHearService;

    @Autowired
    private WorkflowDebugtaxService workflowDebugtaxService;

    @Autowired
    private JcAjxxService jcAjxxService;

    @Autowired
    private JcAjtzSlytzService jcAjtzSlytzService;

    @Autowired
    private DmQxSwrysfService dmQxSwrysfService;

    @Autowired
    private SysUserMapper sysUserMapper;

    @ApiOperation(value = "案件调整")
    @LogAnnotation(title = "审理员调整", action = "审理员-分页查询")
    @PostMapping("/querySlytzPage")
    @ResponseBody

    public DataResult querySlytzPage(@RequestBody JcAjztSlytzEntity jcAjztSlytzEntity) throws ParseException {
        IPage<JcAjztSlytzEntity> iPage = jcAjtzSlytzService.pageInfo(jcAjztSlytzEntity);
        return DataResult.success(iPage);
    }


    @ApiOperation(value = "审理审理员环节-个人待办")
    @LogAnnotation(title = "审理审理员环节", action = "个人待办")
    @PostMapping("/listwait21")
    @RequiresPermissions("workflow:hear:wait21")
    @ResponseBody
    public DataResult listwait21(@RequestBody WorkflowInspectionHearEntity workflowInspectionHear) {
        List<JcAjxxEntity> iPage = jcAjxxService.pageInfo21(workflowInspectionHear);
        return DataResult.success(iPage);

    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @ApiOperation(value = "审理审理员环节-个人已办")
    @LogAnnotation(title = "审理审理员环节", action = "个人已办")
    @PostMapping("/listthen21")
    @RequiresPermissions("workflow:hear:then21")
    @ResponseBody
    public DataResult listthen21(@RequestBody WorkflowInspectionHearEntity workflowInspectionHear) {
        String currentUserId = httpSessionService.getCurrentUserId();

        Page page = new Page(workflowInspectionHear.getPage(), workflowInspectionHear.getLimit());
        LambdaQueryWrapper<WorkflowInspectionHearEntity> queryWrapper = Wrappers.lambdaQuery();
        // 查询条件示例
        queryWrapper.eq(WorkflowInspectionHearEntity::getHearMan21, currentUserId);

        IPage<WorkflowInspectionHearEntity> iPage = workflowInspectionHearService.page(page, queryWrapper);

        List<WorkflowInspectionHearEntity> records = iPage.getRecords();
        for (WorkflowInspectionHearEntity workflowInspectionHearEntity : records) {
            String relevance = workflowInspectionHearEntity.getRelevance();
            String stateCode = workflowInspectionHearEntity.getStateCode();
            String stateName = workflowInspectionHearEntity.getStateName();

            JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(relevance);
            BeanUtils.copyProperties(jcAjxxEntity, workflowInspectionHearEntity);
            workflowInspectionHearEntity.setStateCode(stateCode);
            workflowInspectionHearEntity.setStateName(stateName);
        }

        return DataResult.success(iPage);

    }


    @ApiOperation(value = "执行执行员环节-个人待办")
    @LogAnnotation(title = "执行执行员环节", action = "个人待办")
    @PostMapping("/zxyzxhjgrdb")
    @ResponseBody

    public DataResult zxyzxhjgrdb(@RequestBody WorkflowInspectionHearEntity workflowInspectionHear) {
        List<ZxyhjEntity> iPage = jcAjxxService.zxyzxhjgrdb(workflowInspectionHear);
        return DataResult.success(iPage);
    }


    @SuppressWarnings({"rawtypes", "unchecked"})
    @ApiOperation(value = "审理副审环节-个人待办")
    @LogAnnotation(title = "审理副审环节", action = "个人待办")
    @PostMapping("/listwait22")
    @RequiresPermissions("workflow:hear:wait22")
    @ResponseBody

    public DataResult listwait22(@RequestBody WorkflowInspectionHearEntity workflowInspectionHear) {
        String currentUserId = httpSessionService.getCurrentUserId();

        Page page = new Page(workflowInspectionHear.getPage(), workflowInspectionHear.getLimit());
        LambdaQueryWrapper<WorkflowInspectionHearEntity> queryWrapper = Wrappers.lambdaQuery();
        // 查询条件示例

        queryWrapper.eq(WorkflowInspectionHearEntity::getHearMan22, currentUserId);
        List<String> stateCodeList = new ArrayList<>();
        stateCodeList.add(WorkflowConstants.HEAR_22);
        stateCodeList.add(WorkflowConstants.HEAR_2101);
        queryWrapper.in(WorkflowInspectionHearEntity::getStateCode, stateCodeList);
        IPage<WorkflowInspectionHearEntity> iPage = workflowInspectionHearService.page(page, queryWrapper);

        List<WorkflowInspectionHearEntity> records = iPage.getRecords();
        for (WorkflowInspectionHearEntity workflowInspectionHearEntity : records) {
            String relevance = workflowInspectionHearEntity.getRelevance();
            String stateCode = workflowInspectionHearEntity.getStateCode();
            String stateName = workflowInspectionHearEntity.getStateName();

            JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(relevance);
            BeanUtils.copyProperties(jcAjxxEntity, workflowInspectionHearEntity);
            workflowInspectionHearEntity.setStateCode(stateCode);
            workflowInspectionHearEntity.setStateName(stateName);
        }

        return DataResult.success(iPage);

    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @ApiOperation(value = "审理副审环节-个人已办")
    @LogAnnotation(title = "审理副审环节", action = "个人已办")
    @PostMapping("/listthen22")
    @RequiresPermissions("workflow:hear:then22")
    @ResponseBody

    public DataResult listthen22(@RequestBody WorkflowInspectionHearEntity workflowInspectionHear) {
        String currentUserId = httpSessionService.getCurrentUserId();

        Page page = new Page(workflowInspectionHear.getPage(), workflowInspectionHear.getLimit());
        LambdaQueryWrapper<WorkflowInspectionHearEntity> queryWrapper = Wrappers.lambdaQuery();
        // 查询条件示例
        queryWrapper.eq(WorkflowInspectionHearEntity::getHearMan22, currentUserId);

        IPage<WorkflowInspectionHearEntity> iPage = workflowInspectionHearService.page(page, queryWrapper);

        List<WorkflowInspectionHearEntity> records = iPage.getRecords();
        for (WorkflowInspectionHearEntity workflowInspectionHearEntity : records) {
            String relevance = workflowInspectionHearEntity.getRelevance();
            String stateCode = workflowInspectionHearEntity.getStateCode();
            String stateName = workflowInspectionHearEntity.getStateName();

            JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(relevance);
            BeanUtils.copyProperties(jcAjxxEntity, workflowInspectionHearEntity);
            workflowInspectionHearEntity.setStateCode(stateCode);
            workflowInspectionHearEntity.setStateName(stateName);
        }

        return DataResult.success(iPage);

    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @ApiOperation(value = "审理科长环节-个人待办")
    @LogAnnotation(title = "审理科长环节", action = "个人待办")
    @PostMapping("/listwait23")
    @RequiresPermissions("workflow:hear:wait23")
    @ResponseBody

    public DataResult listwait23(@RequestBody WorkflowInspectionHearEntity workflowInspectionHear) {

        String currentUserId = httpSessionService.getCurrentUserId();

        Page page = new Page(workflowInspectionHear.getPage(), workflowInspectionHear.getLimit());
        LambdaQueryWrapper<WorkflowInspectionHearEntity> queryWrapper = Wrappers.lambdaQuery();
        // 查询条件示例
        queryWrapper.eq(WorkflowInspectionHearEntity::getHearMan23, currentUserId);

        List<String> stateCodeList = new ArrayList<>();
        stateCodeList.add(WorkflowConstants.HEAR_23);
        stateCodeList.add(WorkflowConstants.HEAR_2103);

        queryWrapper.in(WorkflowInspectionHearEntity::getStateCode, stateCodeList);

        IPage<WorkflowInspectionHearEntity> iPage = workflowInspectionHearService.page(page, queryWrapper);

        List<WorkflowInspectionHearEntity> records = iPage.getRecords();
        for (WorkflowInspectionHearEntity workflowInspectionHearEntity : records) {
            String relevance = workflowInspectionHearEntity.getRelevance();
            String stateCode = workflowInspectionHearEntity.getStateCode();
            String stateName = workflowInspectionHearEntity.getStateName();

            JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(relevance);
            BeanUtils.copyProperties(jcAjxxEntity, workflowInspectionHearEntity);
            workflowInspectionHearEntity.setStateCode(stateCode);
            workflowInspectionHearEntity.setStateName(stateName);
        }

        return DataResult.success(iPage);

    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @ApiOperation(value = "审理科长环节-个人待办")
    @LogAnnotation(title = "审理科长环节", action = "个人待办")
    @PostMapping("/listthen23")
    @RequiresPermissions("workflow:hear:then23")
    @ResponseBody

    public DataResult listthen23(@RequestBody WorkflowInspectionHearEntity workflowInspectionHear) {

        String currentUserId = httpSessionService.getCurrentUserId();

        Page page = new Page(workflowInspectionHear.getPage(), workflowInspectionHear.getLimit());
        LambdaQueryWrapper<WorkflowInspectionHearEntity> queryWrapper = Wrappers.lambdaQuery();
        // 查询条件示例
        queryWrapper.eq(WorkflowInspectionHearEntity::getHearMan23, currentUserId);

        IPage<WorkflowInspectionHearEntity> iPage = workflowInspectionHearService.page(page, queryWrapper);

        List<WorkflowInspectionHearEntity> records = iPage.getRecords();
        for (WorkflowInspectionHearEntity workflowInspectionHearEntity : records) {
            String relevance = workflowInspectionHearEntity.getRelevance();
            String stateCode = workflowInspectionHearEntity.getStateCode();
            String stateName = workflowInspectionHearEntity.getStateName();

            JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(relevance);
            BeanUtils.copyProperties(jcAjxxEntity, workflowInspectionHearEntity);
            workflowInspectionHearEntity.setStateCode(stateCode);
            workflowInspectionHearEntity.setStateName(stateName);
        }
        return DataResult.success(iPage);
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @ApiOperation(value = "审理主管局长环节-个人待办")
    @LogAnnotation(title = "审理主管局长环节", action = "个人待办")
    @PostMapping("/listwait24")
    @RequiresPermissions("workflow:hear:wait24")
    @ResponseBody

    public DataResult listwait24(@RequestBody WorkflowInspectionHearEntity workflowInspectionHear) {

        String currentUserId = httpSessionService.getCurrentUserId();

        Page page = new Page(workflowInspectionHear.getPage(), workflowInspectionHear.getLimit());
        LambdaQueryWrapper<WorkflowInspectionHearEntity> queryWrapper = Wrappers.lambdaQuery();
        // 查询条件示例

        queryWrapper.eq(WorkflowInspectionHearEntity::getHearMan24, currentUserId);

        List<String> stateCodeList = new ArrayList<>();
        stateCodeList.add(WorkflowConstants.HEAR_24);
        stateCodeList.add(WorkflowConstants.HEAR_2104);

        queryWrapper.in(WorkflowInspectionHearEntity::getStateCode, stateCodeList);

        IPage<WorkflowInspectionHearEntity> iPage = workflowInspectionHearService.page(page, queryWrapper);

        List<WorkflowInspectionHearEntity> records = iPage.getRecords();
        for (WorkflowInspectionHearEntity workflowInspectionHearEntity : records) {
            String relevance = workflowInspectionHearEntity.getRelevance();
            String stateCode = workflowInspectionHearEntity.getStateCode();
            String stateName = workflowInspectionHearEntity.getStateName();

            JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(relevance);
            BeanUtils.copyProperties(jcAjxxEntity, workflowInspectionHearEntity);
            workflowInspectionHearEntity.setStateCode(stateCode);
            workflowInspectionHearEntity.setStateName(stateName);
        }

        return DataResult.success(iPage);

    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @ApiOperation(value = "审理主管局长环节-个人待办")
    @LogAnnotation(title = "审理主管局长环节", action = "个人待办")
    @PostMapping("/listthen24")
    @RequiresPermissions("workflow:hear:then24")
    @ResponseBody

    public DataResult listthen24(@RequestBody WorkflowInspectionHearEntity workflowInspectionHear) {

        String currentUserId = httpSessionService.getCurrentUserId();

        Page page = new Page(workflowInspectionHear.getPage(), workflowInspectionHear.getLimit());
        LambdaQueryWrapper<WorkflowInspectionHearEntity> queryWrapper = Wrappers.lambdaQuery();
        // 查询条件示例
        queryWrapper.eq(WorkflowInspectionHearEntity::getHearMan24, currentUserId);

        IPage<WorkflowInspectionHearEntity> iPage = workflowInspectionHearService.page(page, queryWrapper);

        List<WorkflowInspectionHearEntity> records = iPage.getRecords();
        for (WorkflowInspectionHearEntity workflowInspectionHearEntity : records) {
            String relevance = workflowInspectionHearEntity.getRelevance();
            String stateCode = workflowInspectionHearEntity.getStateCode();
            String stateName = workflowInspectionHearEntity.getStateName();

            JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(relevance);
            BeanUtils.copyProperties(jcAjxxEntity, workflowInspectionHearEntity);
            workflowInspectionHearEntity.setStateCode(stateCode);
            workflowInspectionHearEntity.setStateName(stateName);
        }

        return DataResult.success(iPage);

    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @ApiOperation(value = "调整审批人-列表")
    @LogAnnotation(title = "调整审批人", action = "列表")
    @PostMapping("/listdebugtaxman")
    @RequiresPermissions("workflow:hear:listdebugtaxman")
    @ResponseBody

    public DataResult listdebugtaxman(@RequestBody WorkflowInspectionHearEntity workflowInspectionHear) {
        String currentDeptId = httpSessionService.getCurrentDeptId();
        String jcajbh = workflowInspectionHear.getJcajbh();
        if (TsBqUtils.isNull(jcajbh)) {
            return DataResult.fail("请输入案件编号！");
        }
        JcAjxxEntity _jcAjxxEntity = jcAjxxService.getByJcajbh(jcajbh);
        if (TsBqUtils.isNull(_jcAjxxEntity)) {
            return DataResult.fail("未查询到此案件！");
        }
        List<String> stateList = new ArrayList<>();
        stateList.add(WorkflowConstants.HEAR_21);
        stateList.add(WorkflowConstants.HEAR_22); // 审理-审理复审岗位
        stateList.add(WorkflowConstants.HEAR_23); // 审理-审理科长岗位
        stateList.add(WorkflowConstants.HEAR_24); // 审理-审理主管局长岗位
        stateList.add(WorkflowConstants.HEAR_211); // 退回-审理-审理员岗位
        stateList.add(WorkflowConstants.HEAR_221); // 退回-审理-审理复审岗位
        stateList.add(WorkflowConstants.HEAR_231); // 退回-审理-审理科长岗位
        stateList.add(WorkflowConstants.HEAR_241); // 退回-审理-审理主管局长岗位
        stateList.add(WorkflowConstants.HEAR_25); // 审理-审理主管局长岗位
        stateList.add(WorkflowConstants.HEAR_26); // 审理-审理会议审岗位
        Page page = new Page(workflowInspectionHear.getPage(), workflowInspectionHear.getLimit());
        LambdaQueryWrapper<WorkflowInspectionHearEntity> queryWrapper = Wrappers.lambdaQuery();
        // 查询条件示例
        queryWrapper.isNull(WorkflowInspectionHearEntity::getDebugtaxman);
        queryWrapper.in(WorkflowInspectionHearEntity::getStateCode, stateList);
        if (TsBqUtils.isNotNull(workflowInspectionHear.getAjmc())) {
            queryWrapper.like(WorkflowInspectionHearEntity::getAjmc, workflowInspectionHear.getAjmc());
        }
        if (TsBqUtils.isNotNull(workflowInspectionHear.getNsrsbh())) {
            queryWrapper.like(WorkflowInspectionHearEntity::getNsrsbh, workflowInspectionHear.getNsrsbh());
        }
        if (TsBqUtils.isNotNull(workflowInspectionHear.getNsrmc())) {
            queryWrapper.like(WorkflowInspectionHearEntity::getAjmc, workflowInspectionHear.getNsrmc());
        }
        queryWrapper.like(WorkflowInspectionHearEntity::getHearTaxorg21, currentDeptId.substring(0, 9));
        queryWrapper.eq(WorkflowInspectionHearEntity::getRelevance, _jcAjxxEntity.getJcajxxuuid());
        IPage<WorkflowInspectionHearEntity> iPage = workflowInspectionHearService.page(page, queryWrapper);
        List<WorkflowInspectionHearEntity> records = iPage.getRecords();
        for (WorkflowInspectionHearEntity workflowInspectionHearEntity : records) {
            String relevance = workflowInspectionHearEntity.getRelevance();
            String stateCode = workflowInspectionHearEntity.getStateCode();
            String stateName = workflowInspectionHearEntity.getStateName();
            JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(relevance);
            BeanUtils.copyProperties(jcAjxxEntity, workflowInspectionHearEntity);
            workflowInspectionHearEntity.setStateCode(stateCode);
            workflowInspectionHearEntity.setStateName(stateName);
        }
        return DataResult.success(iPage);
    }


    @SuppressWarnings({"rawtypes", "unchecked"})
    @ApiOperation(value = "调整审批人-列表(已调整)")
    @LogAnnotation(title = "调整审批人", action = "列表(已调整)")
    @PostMapping("/listdenodebugtaxman")
    @RequiresPermissions("workflow:hear:listdebugtaxman")
    @ResponseBody

    public DataResult listdenodebugtaxman(@RequestBody WorkflowInspectionHearEntity workflowInspectionHear) {
        String currentUserId = httpSessionService.getCurrentUserId();
        Page page = new Page(workflowInspectionHear.getPage(), workflowInspectionHear.getLimit());
        LambdaQueryWrapper<WorkflowDebugtaxEntity> queryWrapper = Wrappers.lambdaQuery();
        // 查询条件示例
        queryWrapper.eq(WorkflowDebugtaxEntity::getCreateId, currentUserId);
        queryWrapper.eq(WorkflowDebugtaxEntity::getType, WorkflowConstants.DEBUGTAX_1);
        IPage<WorkflowDebugtaxEntity> iPage = workflowDebugtaxService.page(page, queryWrapper);
        List<WorkflowDebugtaxEntity> records = iPage.getRecords();
        for (WorkflowDebugtaxEntity workflowDebugtaxEntity : records) {
            String oldinfo = workflowDebugtaxEntity.getOldinfo();
            String newinfo = workflowDebugtaxEntity.getNewinfo();
            String relevance = workflowDebugtaxEntity.getRelevance();
            JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(relevance);
            BeanUtils.copyProperties(jcAjxxEntity, workflowDebugtaxEntity);

            workflowDebugtaxEntity.setOldinfo(sysUserMapper.selectById(oldinfo).getNickName());
            workflowDebugtaxEntity.setNewinfo(sysUserMapper.selectById(newinfo).getNickName());

        }

        return DataResult.success(iPage);
    }

    @ApiOperation(value = "调整审理人-调整")
    @LogAnnotation(title = "调整审理人", action = "调整")
    @PostMapping("/debugtaxman")
    @RequiresPermissions("workflow:hear:debugtaxman")
    @ResponseBody

    public DataResult debugtaxman(@RequestBody WorkflowInspectionHearEntity workflowInspectionHear) {
        String currentUserId = httpSessionService.getCurrentUserId();
        String debugtaxman = workflowInspectionHear.getDebugtaxman();
        String[] debugtaxmanArr = debugtaxman.split("-");
        String identity = debugtaxmanArr[1];
        DmQxSwrysfEntity dmQxSwrysf = dmQxSwrysfService.getUserIdentityByIdentity(identity);
        String content = workflowInspectionHear.getContent();

        Date date = new Date();
        String jcajxxuuid = workflowInspectionHear.getJcajxxuuid();
        WorkflowInspectionHearEntity workflowInspectionHearEntity = workflowInspectionHearService.getById(jcajxxuuid);
        String hearMan21 = workflowInspectionHearEntity.getHearMan21();
        ;
        workflowInspectionHearEntity.setHearMan21(debugtaxmanArr[0]);
        workflowInspectionHearEntity.setHearTaxorg21(dmQxSwrysf.getSfswjgDm());
        workflowInspectionHearEntity.setDebugtaxman(debugtaxmanArr[0]);
        workflowInspectionHearService.updateById(workflowInspectionHearEntity);

        WorkflowDebugtaxEntity workflowDebugtaxEntity = new WorkflowDebugtaxEntity();
        workflowDebugtaxEntity.setId(TsBqUtils.getUUID());
        workflowDebugtaxEntity.setType(WorkflowConstants.DEBUGTAX_1);
        workflowDebugtaxEntity.setRelevance(jcajxxuuid);
        workflowDebugtaxEntity.setNewinfo(debugtaxmanArr[0]);
        workflowDebugtaxEntity.setOldinfo(hearMan21);
        workflowDebugtaxEntity.setContent(content);
        workflowDebugtaxEntity.setDeleted(Constants.DATA_NOT_DELETED);
        workflowDebugtaxEntity.setCreateTime(date);
        workflowDebugtaxEntity.setCreateId(currentUserId);

        workflowDebugtaxService.save(workflowDebugtaxEntity);

        return DataResult.success();
    }

}
